package com.ms.game.sendgift.db;

import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.itembox.Item;
import com.ms.game.itembox.db.ItemDAO;
import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.sendgift.NotifyData;
import com.ms.game.sendgift.SendGiftStage;
import com.ms.game.sendgift.message.SendGiftAllNotifyResponse;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

/**
 * 讀取所有通知列表
 */
public class LoadAllNotifyDataAsync extends TaskAsyncCommand {

	public GUID playerID;
	
	public GUID sessionID;
	
	public SendGiftAllNotifyResponse response;
	
	/**
	 * 給網頁虛寶交易用的
	 */
	private HashMap<Long, ArrayList<PlayerItemModel>> playerItemsForWeb;
	
	@Override
	public void asyncExecute() throws Exception {
		playerItemsForWeb = new HashMap<Long, ArrayList<PlayerItemModel>>();
		ArrayList<SendGiftNotifyModel> notifyList = SendGiftDAO.loadAllSendGiftNotifyModelByPlayerID(playerID);
		for(SendGiftNotifyModel model : notifyList){
			// 取回寄送道具清單
			ArrayList<PlayerItemModel> sendGiftItems = ItemDAO.loadSendGiftItems(model.getOrderID());
			HashMap<String, SSimpleItem> items = new HashMap<String, SSimpleItem>();
			ArrayList<PlayerItemModel> playItemModelList = new ArrayList<PlayerItemModel>();
			for (PlayerItemModel itemModel : sendGiftItems) {
				playItemModelList.add(itemModel);
				Item item = new Item(itemModel);
				items.put(item.getSn(), item.toClientFormat());
			}
			playerItemsForWeb.put(model.getSendOrderID(), playItemModelList);
			if( model.isFinish()){
				//已經被註記成結束的訂單
				if((model.getStage() == SendGiftStage.FINIAL_SEND) && model.getPlayerGUID().equals(playerID.toString())){
					//收的人領取，送的人不需要顯示
					continue;
				}else if((model.getStage() != SendGiftStage.FINIAL_SEND) && model.getTargetGUID().equals(playerID.toString())){
					//階段不是Final taken ， 收的人不需要顯示這筆
					continue;
				}
			}

			//其餘的資料回傳
			NotifyData data = new NotifyData();
			data.orderID = model.getSendOrderID();
			data.orderNumber = model.getOrderNumber();
			data.senderName = model.getPlayerName();
			data.targerName = model.getTargetName();
			if(playerID.toString().equals(model.getPlayerGUID())){
				data.isSender = true;
			}else{
				data.isSender = false;
			}
			
			if(data.isSender){
				//如果是送方 那就顯示對方照片
				data.facePhoto = model.getTargetFacePhoto();
			}else{
				//如果是收方 就顯上送方照片
				data.facePhoto = model.getSenderFacePhoto();
			}
			
			if(data.facePhoto == null){
				data.facePhoto = "head1";
			}
			
			data.goldCoin = model.getGoldCoin();
			data.charge = model.getServiceCharge();
			data.items = items;
			data.stage = model.getStage();
			data.createTime = model.getCreateTime().getTime();
			
			response.notifyData.add(data);
		}
	}

	@Override
	public void execute() throws Exception {
		
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, MessageTypes.S_GIFT_GET_ALL_NOTIFY_RESPONSE, response);
		
	}

	public HashMap<Long, ArrayList<PlayerItemModel>> getPlayerItemsForWeb() {
		return playerItemsForWeb;
	}

	public void setPlayerItemsForWeb(HashMap<Long, ArrayList<PlayerItemModel>> playerItemsForWeb) {
		this.playerItemsForWeb = playerItemsForWeb;
	}

}
